import { NodeSize } from '../config'

export default function tableInput(flow) {
  flow.register('TableInput', ({ PolygonNode, PolygonNodeModel, h }) => {
    class Node extends PolygonNode {
      getIconShape() {
        return h(
          'svg',
          {
            width: NodeSize,
            height: NodeSize,
            viewBox: '0 0 1024 1024'
          },
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M907.824762 800.572952v75.142096l-127.366095-127.390477-32.768 32.768 127.390476 127.414858h-76.678095l0.024381 46.34819 155.721142-0.073143v-154.209524z'
            }
          ),
          h(
            'path',
            {
              fill: '#EFF9FE',
              d: 'M627.370667 404.358095h159.207619v-159.402666H242.907429v159.402666h164.742095v-0.219428h19.504762v0.219428h180.711619v-0.219428h19.504762z'
            }
          ),
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M767.439238 604.574476h-140.068571v-180.687238h159.207619v161.52381l41.447619-41.447619V203.50781h-626.590476v626.566095h340.455619l41.423238-' +
                '41.423238H427.154286v-164.571429h180.711619v140.044191l19.504762-19.504762v-120.539429h120.539428l19.529143-19.504762z m-359.789714 184.076191H242.907429v-' +
                '164.571429h164.766476v164.571429z m0-184.076191H242.907429v-180.687238h164.766476v180.687238z m200.216381 0H427.154286v-180.687238h180.711619v180.687238z m0-' +
                '200.435809v0.219428H427.154286v-0.219428h-19.504762v0.219428H242.907429v-159.402666h543.695238v159.402666h-159.207619v-0.219428h-19.529143z'
            }
          )
        )
      }

      getLabelShape() {
        return h(
          'text',
          {
            fill: '#000000',
            fontSize: 12,
            x: NodeSize / 2,
            y: NodeSize + 20,
            textAnchor: 'middle',
            dominantBaseline: 'middle'
          },
          '表输入'
        )
      }

      getShape() {
        const { x, y, width, height } = this.getAttributes()
        const transform = `matrix(1 0 0 1 ${x - width / 2} ${y - height / 2})`
        return h(
          'g',
          {
            transform
          },
          [
            h(
              'rect',
              {
                rx: 5,
                ry: 5,
                width: NodeSize,
                height: NodeSize,
                fill: '#fff',
                stroke: '#3D6380',
                strokeWidth: 1
              }
            ),
            this.getIconShape(),
            this.getLabelShape()
          ]
        )
      }
    }
    class Model extends PolygonNodeModel {
      constructor(data, graphModel) {
        super(data, graphModel)
        this.points = [
          [NodeSize / 2, 0],
          [NodeSize, NodeSize / 2],
          [NodeSize / 2, NodeSize],
          [0, NodeSize / 2]
        ]
      }
    }

    return {
      view: Node,
      model: Model
    }
  })
}
